/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》http://lblbc.cn/blog
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 **/
var app = getApp()
var interval = 0
var isStarted = false
var redBalls = []
Page({
  data: {
    startPauseButtonText: '开始',
    ball1: '15',
    ball2: '15',
    ball3: '15',
    ball4: '15',
    ball5: '15',
    ball6: '15',
    ball7: '15',
  },
  initRedBalls() {
    redBalls = []
    for (let i = 1;i <= 33; i++) {
      redBalls.push(i)
    }
  },
  startOrPauseTimer() {
    if (isStarted) {
      if (interval != 0) {
        clearInterval(interval)
        interval = 0
      }
      this.setData({
        startPauseButtonText: '开始'
      })
    } else {
      this.setData({
        startPauseButtonText: '暂停'
      })

      if (interval == 0) {
        interval = setInterval(() => {
          this.initRedBalls()
          this.setData({
            ball1: this.getRandomRedBall(),
            ball2: this.getRandomRedBall(),
            ball3: this.getRandomRedBall(),
            ball4: this.getRandomRedBall(),
            ball5: this.getRandomRedBall(),
            ball6: this.getRandomRedBall(),
            ball7: this.getRandom(16)
          })
        }, 100)
      }
    }

    isStarted = !isStarted
  },
  getRandom(maxNum) {
    return Math.floor(Math.random() * maxNum)+1
  },
  getRandomRedBall(){
    var arrSize = redBalls.length
    var numIndex = this.getRandom(arrSize)-1
    var num = redBalls[numIndex]
    redBalls.splice(numIndex,1)
    return num
  }
})
